﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Course.Entity;
using PowerCreator.LMS.Web.Paper;
using PowerCreator.LMS.Paper.Entiry;
using PowerCreator.LMS.Web;

public partial class sysadmin_CourseManager_AddCoursePaper : PowerCreator.LMS.Web.Base.BasePage
{
    Course opCourse = new Course();
    ManagerPaper opPaper = new ManagerPaper();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();
            BinderResourceType();
            int courseid = 0;
            int paperid = 0;
            if (Request.QueryString["CourseID"] != null)
            {
                courseid = int.Parse(Request.QueryString["CourseID"].ToString());
                ViewState["CourseID"] = courseid.ToString();
                paperid = int.Parse(Request.QueryString["PaperID"].ToString());
                ViewState["PaperID"] = paperid.ToString();
                BinderForm();
            }
        }
    }
    protected override void InitPage()
    {
        lbl_InfoTitle_AddCoursePaper.Text = GetLanageValueByKey("AddCoursePaper_msg_m1", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_Course.Text = GetLanageValueByKey("ResourceSelect_msg_m2", PowerCreator.LMS.Language.LanagetItemType.Page) + "：";
        lbl_Paper.Text = GetLanageValueByKey("AddCoursePaper_msg_m2", PowerCreator.LMS.Language.LanagetItemType.Page) + "：";
        lbl_ResourceType.Text = GetLanageValueByKey("AddCoursePaper_msg_m3", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_CoursersStartTime.Text =GetLanageValueByKey("lbl_AddClass_TrainclaStartTime", PowerCreator.LMS.Language.LanagetItemType.Page) ;
        lbl_CoursersEndTime.Text = GetLanageValueByKey("lbl_AddClass_TrainclaEndTime", PowerCreator.LMS.Language.LanagetItemType.Page);

        btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnReset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }
    private void BinderResourceType()
    {
        DataTable dt = new DataTable();
        dt = opCourse.QueryResourceType("");
        //dt = objCourse.Query("");
        ddl_CoursersRestpID.DataSource = dt;
        ddl_CoursersRestpID.DataTextField = "RestpName";
        ddl_CoursersRestpID.DataValueField = "RestpID";
        ddl_CoursersRestpID.DataBind();

    }
    private void BinderForm()
    {
        VOPaper vop = new VOPaper();
        VOTOCourse voc = new VOTOCourse();
        vop = opPaper.LoadPaper(int.Parse(ViewState["PaperID"].ToString()));
        voc = opCourse.LoadVOTCourse(int.Parse(ViewState["CourseID"].ToString()));
        lbl_CourseName.Text = voc.CourseName;
        lbl_PaperName.Text = vop.PaperName;
    }
    private bool CheckPageData()
    {
        string searcht = string.Format("FKCourseresCourseID={0} and FKCoursersPaperID={1} ", ViewState["CourseID"], ViewState["PaperID"]);
        if (opCourse.QueryCourseResourceCount(searcht) > 0)
        {
            //lbl_msg_Error.Text = GetLanageValueByKey("lbl_msg_addjob_aspx_count", PowerCreator.LMS.Language.LanagetItemType.MSG);
            lbl_msg_Error.Text = "<br />" + GetLanageValueByKey("AddCoursePaper_msg_m4", PowerCreator.LMS.Language.LanagetItemType.Page);
            return false;
        }
        TimeSpan ts = PowerCreator.LMS.Common.Utils.TimeUtils.TimeCompart(DateTime.Parse(txt_CoursersStartTime.Text), DateTime.Parse(txt_CoursersEndTime.Text));
        if (ts.Ticks < 0 )
        {
            lbl_msg_Error.Text = "<br />" + GetLanageValueByKey("AddCoursePaper_msg_m5", PowerCreator.LMS.Language.LanagetItemType.Page);
            return false;
        }   
        return true;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;
      
        VOTResourceType voResourceType = new VOTResourceType();
        voResourceType.RestpID = int.Parse(ddl_CoursersRestpID.SelectedItem.Value);
        voResourceType.RestpName = ddl_CoursersRestpID.SelectedItem.Text;

        CreatedByInfo cinfo = GetCreatedByInfo();
        VOTOCourse voCourse = opCourse.LoadVOTCourse(int.Parse(ViewState["CourseID"].ToString()));
        CourseresourcePaperInfo paperCourseResource = new CourseresourcePaperInfo();
        VOPaper voPaper = opPaper.LoadPaper(int.Parse(ViewState["PaperID"].ToString()));
        paperCourseResource.PaperID = voPaper.PaperID;
        paperCourseResource.PaperName = voPaper.PaperName;
        paperCourseResource.CoursersIndex = 0;
        paperCourseResource.CoursersEndTime = DateTime.Parse(txt_CoursersEndTime.Text);
        paperCourseResource.CoursersStartTime = DateTime.Parse(txt_CoursersStartTime.Text);

        if (opCourse.AddCourseResource(null, voResourceType, cinfo, voCourse,null, paperCourseResource, CoursersClass.Paper))
        {
            string url = "ResourceList.aspx?CourseID=" + ViewState["CourseID"].ToString();
            Script.RedirectURL(url);
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        string url = "ResourceList.aspx?CourseID=" + ViewState["CourseID"].ToString();
        Script.RedirectURL(url);
    }
}
